package com.tingyuge.thinking.spring.configuration.metadata;

import com.tingyuge.thinking.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

/**
 *  user 元素的 {@link org.springframework.beans.factory.xml.BeanDefinitionParser} 实现
 * @author xiaodao
 * @version 1.0
 * @date 2021/8/22
 */
public class UserBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
    @Override
    protected Class<?> getBeanClass(Element element) {
        return User.class;
    }

    @Override
    protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
        String id = element.getAttribute("id");
        if(StringUtils.hasText(id)){
            builder.addPropertyValue("id", id);//相当于 BeanDefinition中定义了id
        }
        setPropertyValue("name",element,builder);


    }
    private void setPropertyValue(String  attributeName, Element element ,BeanDefinitionBuilder builder){
        String value = element.getAttribute(attributeName);
        if(StringUtils.hasText(attributeName)){
            builder.addPropertyValue(attributeName,value);
        }
    }

}



